package com.ilovepostcard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ilovepostcard.controller.ServerAsyncTask;
import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.DrawableManager;
import com.ilovepostcard.view.MyProfileView;

public class MyActivity extends MenuActivity {
    
    public static final int REQUEST_CODE_EDIT_PROFILE = 2012;
    
    MyProfileView myProfileView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        myProfileView = (MyProfileView) findViewById(R.id.my_profile_view);
        setupUser();
    }

    public void onDeletePubSuccess() {
    	myProfileView.onDeletePubSuccess();
    }
    
    private void setupUser() {
        User user = UserController.getDetailUser(this);
//        if (user != null) {
//            myProfileView.setUser(user);
//        } else {
            new ServerAsyncTask(this) {

            	protected boolean isShowDialog() {
            		return false;
            	}
            	
                @Override
                protected void onSuccess(PostcardResult result) {
                    User user = result.getUser();
                    myProfileView.setUser(user);
                    UserController.saveDetailUser(MyActivity.this, user);
                }

                @Override
                protected PostcardResult doRequest() {
                    User user = UserController.getLoginUser(MyActivity.this);
                    return ServerFactory.getServer().getUserInfo("", user.UserID);
                }
            }.execute();
//        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_EDIT_PROFILE) {
            if (resultCode == Activity.RESULT_OK) {
                setupUser();
            } else if (resultCode == Activity.RESULT_FIRST_USER) {
                new ServerAsyncTask(this) {

                    @Override
                    protected void onSuccess(PostcardResult result) {
                        User user = result.getUser();
                        if (user.HasHeadImg) {
                            DrawableManager.getInstance().clearCache(user.HeadImgUrl);
                        }
                        myProfileView.setUser(user);
                        UserController.saveDetailUser(MyActivity.this, user);
                    }

                    @Override
                    protected PostcardResult doRequest() {
                        User user = UserController.getLoginUser(MyActivity.this);
                        return ServerFactory.getServer().getUserInfo("", user.UserID);
                    }
                }.execute();
            }
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
